import Vuex from 'vuex'
import defaultState from './state/state'
import mutations from './mutations/mutaions'
import getters from './getters/getters'
import actions from './actions/actions'

const isDev = process.env.NODE_ENV === 'development'
 
export default () => {
  const store = new Vuex.Store({ //hot moudle
    // return new Vuex.Store({
    strict: isDev, //this.$store.state.count = 99; 开发环境限制这样的写法,规范团队
    state: defaultState, //全局模块
    mutations,
    getters,
    actions,
    modules: {
      a: {
        namespaced: true, //命名空间，不设置在全局的mutations下,提升规范性
        state: {
          text: 1
        },
        mutations: {
          updateText(state, text) {
            console.log('a.stata', state)
            state.text = text
          }
        },
        getters: {
          // textPlus(state) { //只依赖a模块的值
          //   return state.text + 1
          // }
          textPlus(state, getters, rootState) { //依赖全局模块的值 ,getters是获取全局的getters，rootState获取全局的state
            // return state.text + rootState.count // 获取顶层模块的
            return state.text + rootState.b.text  //获取b模块的
          }
        },
        actions: {
          add({ state, commit, rootState }) {
            // commit('updateText', rootState.count)  // commit默认获取本模块a 的mutations
            commit('updateCount', { num: 567 }, { root: true }) // 调用全局的mutations
          }
        }
      },
      b: {
        state: {
          text: 2
        },
        actions: {
          testAction({ state, commit, rootState }) {
            commit('a/updateText', 'test text', { root: true }) // 调用a的mutations
          }
        }
      }
    }
  })